<template>
	<view>
			<view class="login-html">
				
					
		<div class="centered-rectangle">
				<!-- 您可以在此区域内添加内容，例如文本、图片或链接 -->
				<div class="avatar-container">
				    <img src="/static/logo2.png" alt="头像" class="avatar">
					
				</div>
				<view class="avatar-info">
					<span class="avatar-icon"><image src="../../../static/人物.png"></image></span>
					<input type="text"
					  placeholder="企业Id"
					  v-model="enterpriseId"
					  class="contact-info"></input>
				</view>
				<view class="avatar-info">
					<span class="avatar-icon"><image src="../../../static/人物.png"></image></span>
					<!-- <u-icon name="man-add-fill" color="#2979ff" size="28"></u-icon> -->
					<input type="text"
					  placeholder="用户名"
					  v-model="phone"
					  class="contact-info2"></input>
				</view>
				<view class="avatar-info">
					<span class="avatar-icon"><image src="../../../static/密码.png"></image></span>
			  <!-- <u--input
			    placeholder="请输入密码"
			    :suffixIcon="currentImage"
			    suffix-icon-style="color: #909399; width: 24px; height: 24px;"
			    v-model="password"
				:type="passwordType" 
			    @password-toggle="togglePassword" 
			    class="contact-info3"  
			  >
			  </u--input> -->
	    <u-input 
	        placeholder="请输入密码" 
	        v-model="password" 
			:customStyle="{ 
				 backgroundColor: '#ffffff',
				 height: '60px',
				 borderRadius: '2px',
				 marginLeft: '20px',
				 width: '300px',
				 position: 'relative',
				 top: '-30px',
				 }"
	        :password="passwordType">
	        <!-- <u-text text="重复密码" style="font-size: 16px;width: 140rpx;" slot="prefix" margin="0 3px 0 0"></u-text> -->
	        <template slot="suffix">
	            <span style="width: 80px;height: 70px;display: inline-block;margin-left: 5rpx;" @click="changeImage()">
	                <image style="width: 40rpx;height: 40rpx;margin-right: 5rpx; z-index: 999;position: relative;top: 26px;left: 10px;"
	                    :src="showPassWord2?'../../../static/眼睛.png':'../../../static/闭眼.png'">
	                </image>
	            </span>
	        </template>
	    </u-input>
					<!-- <input :type="passwordType" id="password"
					  placeholder="密码"
					  v-model="password"
					  class="contact-info3" ></input>
					  <span class="avatar-eyes" @click="togglePassword"><image :src="currentImage" @click="changeImage" style="position: relative;top: 25px;"></image></span> -->
				</view>
				
				 <view class="container" @change="toggleRememberMe">
				    <checkbox-group>
				      <label  class="checkbox-item">
				        <checkbox value="true" checked="true" class="checkbox-checked"  :checked="rememberMe">保存密码</checkbox>
				      </label>
				    </checkbox-group>
				  </view>
				  <div class="wrapper-button">
				  	<button @click="shifu()">登录</button>
				  </div>
		</div>
		<span style="font-size: 12px;color: white;text-align: center;position: fixed;left: 28%;top: 90%;">非凡智联科技有限公司技术支持</span>
	</view>
		</view>
</template>

<script>
	import {goPhoneCode,driverLogin} from '@/api/login.js';
	import { setToken } from '@/util/auth.js'; 
	import { setEenterpriseId } from '@/util/auth.js'; 
	export default {
		data() {
			return {
				
				checkedValues: [], // 存储选中的值
				phone: '',
				password: '',
				enterpriseId:'',
				userName:'',
				userId:'',
				req:{
					phone: '',
					password: '',
					enterpriseId:'',
				},
				
				rememberMe: true,
				subaccount:false,//是否
				// currentImage: '../../../static/闭眼.png',
				currentImage:'map-fill',
				images: ['../../../static/眼睛.png','../../../static/闭眼.png'],
				showPassWord2:false,
				passwordType: true, // 初始化为'password'
			}
		},
		onLoad: function () {
		        // 使用 Promise.all 或 async/await 来并行读取多个值
		        uni.getStorage({
		            key: 'password_key',
		            success: (res)=> {
		                let password = res.data;
						this.password=password;
						// this.password=res.data
		              
		                
		                // 继续读取下一个值
		                uni.getStorage({
		                    key: 'username_key',
		                    success: (res)=> {
		                        let username = res.data;
								this.phone=username;
		                      
		                        
		                        // 继续读取下一个值
		                        uni.getStorage({
		                            key: 'userRole_key',
		                            success: (res)=> {
		                                let userRole = res.data;
										this.enterpriseId=res.data
		                                
		                                // 在这里处理读取到的数据，比如填充到页面的表单中
		                            },
		                            fail: function (err) {
		                                console.error('读取用户角色失败', err);
		                            }
		                        });
		                    },
		                    fail: function (err) {
		                        console.error('读取用户名失败', err);
		                    }
		                });
		            },
		            fail: function (err) {
		                console.error('读取密码失败', err);
		            }
		});
		},
		methods: {
			confirm(e) {
							console.log(e);
						},
			 changeImage() {
				let currentIndex = this.images.indexOf(this.currentImage);
				      let nextIndex = (currentIndex + 1) % this.images.length;
				      this.currentImage = this.images[nextIndex];
				 
				      // 切换 showPassWord2 和 passwordType 的值
				      this.showPassWord2 = !this.showPassWord2; // 使用布尔逻辑来切换
				      this.passwordType = !this.passwordType;   // 同样使用布尔逻辑

			  },
			radioChange(e) {
			      this.selectedOption = e.detail.value;
			    },
			toggleRememberMe(event) {
			      this.rememberMe = event.detail.value;
			    },
			togglePassword() {
			   this.passwordType = this.passwordType === 'password' ? 'text' : 'password';
			},
			shifu(){
				this.req.enterpriseId=this.enterpriseId;
				this.req.password=this.password;
				this.req.phone=this.phone;
				setEenterpriseId(this.enterpriseId)
				goPhoneCode(this.req).then(res =>{
					this.userName=res.data.data.userName
					this.phone=res.data.data.phone
					this.userId=res.data.data.userId
					if(res.data.data.subaccount==null){
						this.subaccount=false;
					}else{
						this.subaccount=true;
					}
					// 请求成功
					
					if (res.statusCode === 200 || rememberMe===true) {
						const token=res.data.data.token;
						setToken(token);
						let enterpriseId=this.enterpriseId;
						let password=this.password;
						let phone=this.phone;
					     uni.setStorage({
					         key: 'password_key',
					         data: password,
					         success: function () {
					             console.log('密码保存成功');
					             // 继续保存下一个值
					             uni.setStorage({
					                 key: 'username_key',
					                 data: phone,
					                 success: function () {
					                     console.log('用户名保存成功');
					                     // 继续保存下一个值
					                     uni.setStorage({
					                         key: 'userRole_key',
					                         data: enterpriseId,
					                         success: function () {
												 
												 let accounts={phone: phone, password: password, enterpriseId: enterpriseId}
												 const storedAccounts = uni.getStorageSync('accounts') || [];
												 // 检查是否已存在具有相同手机号的账户
												 const isAccountExists = storedAccounts.some(account => account.phone === accounts.phone);
												  
												 if (!isAccountExists) {
												 storedAccounts.push(accounts)
												 uni.setStorageSync('accounts',storedAccounts);
					                             console.log('用户角色保存成功'+accounts.phone);
												 }
					                         },
					                         fail: function (err) {
					                             console.error('保存用户角色失败', err);
					                         }
					                     });
					                 },
					                 fail: function (err) {
					                     console.error('保存用户名失败', err);
					                 }
					             });
					         },
					         fail: function (err) {
					             console.error('保存密码失败', err);
					         }
					     });
						 }
							// 创建一个对象来包含所有要存储的参数
							let userParams = {
								userName: this.userName,
								phone: this.phone,
								userId: this.userId,
								subaccount: this.subaccount
							};
							 
							// 使用uni.setStorage来存储这个对象
							uni.setStorage({
								key: 'userParams_key', // 使用一个唯一的键来存储这些参数
								data: userParams,
								success: function () {
									console.log('用户参数保存成功');
									// 这里可以继续执行其他逻辑，比如跳转到某个页面或者显示一个提示信息
								},
								fail: function (err) {
									console.error('保存用户参数失败', err);
								}
							});
					    uni.reLaunch({
					        url: '/pages/index/index?userName='+this.userName+'&phone='+this.phone+'&userId='+this.userId+'&subaccount='+this.subaccount,// 请注意，这里的url应为相对路径，且以'/'开头
					    });
					    uni.showToast({
					        title: '登录成功',
					        icon: 'success',
					    });
				}).catch((error) => {
				    // 请求失败
				    console.error('登录失败', error);
				    uni.showToast({
				        title: '企业ID或账号或密码错误，请重试',
				        icon: 'none',
				    });
				});
			},
			// driver(){
			// 	this.req.enterpriseId=this.enterpriseId;
			// 	this.req.password=this.password;
			// 	this.req.phone=this.phone;
			// 	driverLogin(this.req).then(res =>{
			// 		this.userName=res.data.data.userName
			// 		this.phone=res.data.data.phone
			// 		this.userId=res.data.data.userId
			// 		if(res.data.data.subaccount==null){
			// 			this.subaccount=false;
			// 		}else{
			// 			this.subaccount=true;
			// 		}
			// 		console.log(this.subaccount)
			// 		// 请求成功
					
			// 		if (res.statusCode === 200 || rememberMe===true) {
			// 			const token=res.data.data.token;
			// 			setToken(token);
			// 			let enterpriseId=this.enterpriseId;
			// 			let password=this.password;
			// 			let phone=this.phone;
			// 		     uni.setStorage({
			// 		         key: 'password_key',
			// 		         data: password,
			// 		         success: function () {
			// 		             console.log('密码保存成功');
			// 		             // 继续保存下一个值
			// 		             uni.setStorage({
			// 		                 key: 'username_key',
			// 		                 data: phone,
			// 		                 success: function () {
			// 		                     console.log('用户名保存成功');
			// 		                     // 继续保存下一个值
			// 		                     uni.setStorage({
			// 		                         key: 'userRole_key',
			// 		                         data: enterpriseId,
			// 		                         success: function () {
			// 									 let accounts={phone: this.phone, password: this.password, enterpriseId: this.enterpriseId}
			// 									 const storedAccounts = uni.getStorageSync('accounts') || [];
			// 									 storedAccounts.push(accounts)
			// 									 uni.setStorageSync('driver',storedAccounts);
			// 		                             console.log('用户角色保存成功');
			// 		                         },
			// 		                         fail: function (err) {
			// 		                             console.error('保存用户角色失败', err);
			// 		                         }
			// 		                     });
			// 		                 },
			// 		                 fail: function (err) {
			// 		                     console.error('保存用户名失败', err);
			// 		                 }
			// 		             });
			// 		         },
			// 		         fail: function (err) {
			// 		             console.error('保存密码失败', err);
			// 		         }
			// 		     });
			// 			 }
			// 		    uni.redirectTo({
			// 		        url: '/pages/index/index?userName='+this.userName+'&phone='+this.phone+'&userId='+this.userId+'&subaccount='+this.subaccount+'&selectedOption='+this.selectedOption ,// 请注意，这里的url应为相对路径，且以'/'开头
			// 		    });
			// 		    uni.showToast({
			// 		        title: '登录成功',
			// 		        icon: 'success',
			// 		    });
			// 	}).catch((error) => {
			// 	    // 请求失败
			// 	    console.error('登录失败', error);
			// 	    uni.showToast({
			// 	        title: '账号或密码错误，请重试',
			// 	        icon: 'none',
			// 	    });
			// 	});
			// },
			   checkboxChange(e) {
			      this.rememberMe = e.detail.value;
			    },
			    savePassword() {
			},
			
		}
	}
</script>

<style>
@import "login.css";
</style>
